
import Variables from './variables'

export default function (app) {
  let axios = app.$axios;
 // 基本配置
  axios.defaults.timeout = 10000
  axios.defaults.headers.post['Content-Type'] = 'application/json'
  axios.defaults.headers.common['Authorization'] = Variables.VAR.defaultToken;
  axios.defaults.baseURL = Variables.URL.baseURL


  axios.defaults.transformResponse = [function (data) {
    if (typeof data === 'string') {
      try {
        let d = data;
        let array = d.match(/:\d{14}\d*/g);
        if (array === null) {
          data = JSON.parse(data);
          return data
        }
        for (let i = 0; i < array.length; i++) {
          let str = array[i];

          let number = str.replace(":", "");
          d = d.replace(str, ':"' + number + '"');
        }
        data = JSON.parse(d)
      } catch (e) { /* Ignore */
      }
    }
    return data;
  }];

  // 请求回调
  axios.onRequest(config => {
    config.headers['source'] = 'mall';
    config.headers['Authorization'] = Variables.VAR.defaultToken
  })

  // 返回回调
  axios.onResponse(res => {

  })

  // 错误回调
  axios.onError(error => {

  })
}
